package com.gz.DroidPong.Scenes;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.graphics.Color;
import android.graphics.Typeface;
import android.provider.Contacts.Intents.UI;
import android.util.Log;

import com.gz.DroidPong.Constants.SceneManager;
import com.gz.DroidPong.Constants.UIManager;
import com.gz.Game.Extension.AsyncTaskLoader;
import com.gz.Game.Extension.GameScene;
import com.gz.Game.Extension.IAsyncCallback;

public class LoadingScene extends GameScene {

	public static final int TARGET_MENU_SCENE = 0;
	public static final int TARGET_GAME_SCENE = 0;

	private Engine mEngine;
	
    public LoadingScene(int pLayerCount, Engine baseEngine, int pTargetScene) {
        super(pLayerCount, baseEngine);
                
        mEngine = baseEngine;
        
        log("Init");
    }

    // ===========================================================
    // Fields
    // ===========================================================

    private Texture _loadingTexture;

    private TiledTextureRegion _loadingTextureRegion;
    private TextureRegion _loadTextTextureRegion;
    private Font mFont;
    private ChangeableText mText;
    // ===========================================================
    // Constants
    // ===========================================================

    // ===========================================================
    // Inherited Methods
    // ===========================================================

    @Override
    public void onLoadResources() {
    	log("Loading Resources");
    	
    	
    	
    	
    	
    	
    	
    	
    	
//    	if(GameOptions.GetBoolOption(GameOptions.PREF_BOOL_LOW_RES)){
//    		TextureRegionFactory.setAssetBasePath("gfx_lowres/");
//    	}
//    	else{
//    		TextureRegionFactory.setAssetBasePath("gfx/");
//    	}
        
    	TextureRegionFactory.setAssetBasePath("gfx/");

        _loadingTexture = new Texture(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        _loadingTextureRegion = TextureRegionFactory.createTiledFromAsset(_loadingTexture, UIManager.GetGame(), "loading_tiled.png", 0, 0, 4, 3);
        _loadTextTextureRegion = TextureRegionFactory.createFromAsset(_loadingTexture, UIManager.GetGame(), "loading.png",0,150);
        Texture fontTexture = new Texture(256,256);
        mFont = new Font(fontTexture, Typeface.create(
				Typeface.DEFAULT, Typeface.BOLD), 9, true, Color.LTGRAY);
        this._engine.getTextureManager().loadTextures(_loadingTexture, fontTexture);
        this._engine.getFontManager().loadFont(mFont);
    }

    @Override
    public void onLoadScene() {
    	log("Loading Scene");
    	this.setBackground(new ColorBackground(.2f,.2f,.2f));

    	//94*21
    	//50*50 tiles(4*3)
    	
    	float w = UIManager.getWidth()/4;
    	float h = w;
    	
        AnimatedSprite loader = new AnimatedSprite(0, 0,w,h, _loadingTextureRegion);
        loader.setPosition((UIManager.getWidth() / 2) - (loader.getWidthScaled() / 2), (UIManager.getHeight() / 4));
        loader.animate(85, true);

        w = UIManager.getWidth()/3;
        h = w/5;
        
        Sprite loading = new Sprite(0,0,w,h,_loadTextTextureRegion);
        float widthDiff = loading.getWidth() - loader.getWidth();
        loading.setPosition(loader.getX() - widthDiff/2, loader.getY() + loader.getHeight() + loading.getHeight());
        
        mText = new ChangeableText(0,0,mFont,"Loading...",200);
        mText.setPosition(20,UIManager.getHeight() - 2*mText.getHeight());
        
        this.getLastChild().attachChild(loader);
        this.getLastChild().attachChild(loading);
        this.getLastChild().attachChild(mText);
        
        SceneManager.setScene(SceneManager.SCENE_MENU, new DroidPongScene(3,this._engine));
        
        log("Load resources");
		IAsyncCallback callback = new IAsyncCallback() {
			
			public void workToDo() {
				UIManager.LoadTextures(mEngine);
			}
			
			public void onComplete() {
				SceneManager.getMenuScene().onLoadScene();
			}
		};
		
		new AsyncTaskLoader().execute(callback);
        
        
        SceneManager.setScene(SceneManager.SCENE_GAME, new DroidPongScene(3, this._engine));
        SceneManager.setScene(SceneManager.SCENE_OPTIONS,new DroidPongScene(3, this._engine));
        
        registerUpdateHandler(new IUpdateHandler() {

			public void onUpdate(float pSecondsElapsed) {
				mText.setText(UIManager.getLoadState());
			}

			public void reset() {
				
			}
        	
        });
    }

    @Override
    public void unloadScene() {
    	log("Unload");
    }

    @Override
    public void onLoadComplete() {
    	log("Load Complete");
    }

    // ===========================================================
    // Methods
    // ===========================================================

    private void log(String msg){
    	Log.d("Loading Scene", msg);
    }
    
}